package servlets.site;

import gestionnaires.GestionnaireDeFilms;
import gestionnaires.GestionnaireTags;
import utilisateurs.modeles.Film;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilisateurs.modeles.Tag;
import helpers.ServletHelper;
import java.util.Collection;
import views.TagLightView;

/*
 * Cette servlet permet de faire les traitements sur les tags
 */

@WebServlet(name = "TagServlet", urlPatterns = {"/TagServlet"})
public class TagServlet extends HttpServlet {

    ///////////////////////////GESTIONNAIRES//////////////////////////////
    @EJB
    GestionnaireTags tagFacade;
    @EJB
    GestionnaireDeFilms jeuVideoFacade;
    ////////////////////////////////////////////////////////////////////////

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = ServletHelper.getIntegerValue(request.getParameter("id"));
        String forwardTo = "";


        if (id > 0) {
            Tag tag = tagFacade.find(id);

            // Si le tag existe,
            if (tag != null) {
                // on récupère les parametres
                String start = request.getParameter("start");
                String sort = ServletHelper.getValue(request.getParameter("sort"), "releaseDate");
                String value = ServletHelper.getValue(request.getParameter("value"), "desc");
                // on calcule le nombre de pages nécessaires à l'affichage des jeux assimilés au tags
                int totalPages = ServletHelper.getTotalPages(jeuVideoFacade.countByTag(tag), ServletHelper.JEUXVIDEO_PAR_PAGE);
                int debut = ServletHelper.getStart(start, totalPages);
                // on récupère les jeux videos pour les envoyer dans la réponse
                Collection<Film> jeuxVideo = jeuVideoFacade.getByTag(tag, debut * ServletHelper.JEUXVIDEO_PAR_PAGE, ServletHelper.JEUXVIDEO_PAR_PAGE, sort, value);
                request.setAttribute("tagLV", new TagLightView(tag, jeuxVideo));
                request.setAttribute("totalPages", totalPages);
            } else {
                forwardTo = "?page=error";
            }
        }       
        RequestDispatcher dispatcher = request.getRequestDispatcher("home.jsp" + forwardTo);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
